<?php
namespace App\Controllers\Admin;
use Illuminate\Support\Facades\View;
use Pointconfig;
use Input, Notification, Redirect, Sentry, Str;

class PointconfigsController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * GET /admin/mounts
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        $METHOD = 'pointconfigs';
        $ACTION = 'index';

        $user = Sentry::getUser();
        $pointconfigs = Pointconfig::where('display',1)->get();
        if ($user->hasAccess('admin')){
            return View::make('admin.pointconfigs.index',compact('pointconfigs','METHOD','ACTION'));
        }elseif($user->hasAccess('city')){
            foreach($pointconfigs as $pointconfig){
                $percent = unserialize($pointconfig->city_percent);
                $new_data[$pointconfig->id]['new_point'] = $this->getcount($percent,$pointconfig->count,$user->city);
                $new_data[$pointconfig->id]['percent'] = $percent[$user->city];
            }
            return View::make('admin.pointconfigs.city_index',compact('pointconfigs','METHOD','ACTION','user','new_data'));
        }
	}

	/**
	 * Show the form for creating a new resource.
	 * GET /admin/mounts/create
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 * POST /admin/mounts
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 * GET /admin/mounts/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 * GET /admin/mounts/{id}/edit
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 * PUT /admin/mounts/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 * DELETE /admin/mounts/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

    public function ajax(){
        $type = Input::get('type');
        $id = Input::get('id');
        $user = Sentry::getUser();
        $pointconfig = Pointconfig::find($id);
        if($type == 'city'){
            $per = Input::get('per');
            $data = $pointconfig->city_percent;
            if($data == ''){
                $arr = array($user->city=>$per);
                $city_percent = serialize($arr);
            }else{
                $arr = unserialize($data);
                $arr[$user->city] = $per;
                $city_percent = serialize($arr);
            }
            $pointconfig->city_percent = $city_percent;
            $pointconfig->save();
            echo 1;
        }else{
            $count = Input::get('count');
            $pointconfig->count = $count;
            $pointconfig->save();
            echo 1;
        }
    }


    public function getcount($arr,$count,$city){
        if(isset($arr[$city])){
            $new_count = (float)$count;
            $new_count = $new_count*$arr[$city];
            $new_count = round($new_count);
            return $new_count;
        }else{
            return $count;
        }
    }

}